home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr09 / vstsrc.zip / PRF.C < prev    next >
C/C++ Source or Header  |  1995-01-26  |  3KB  |  129 lines

  1. /*
  2.  * %W% %E% %U%
  3.  * Debugging printf-like functions, assert, etc.
  4.  */
  5.  
  6.  
  7. #include <windows.h>
  8. #include <stdio.h>
  9. #ifdef WIN32
  10. #include <wincon.h>
  11. #else
  12. #include <stdarg.h>
  13. #endif
  14.  
  15. #ifdef _DEBUG_
  16. static char buf[256+2];
  17. static char *bufp = buf, *endp = buf+256;
  18. static HANDLE Console_Handle;
  19. int debugflag = 1;        /* Turn printfs off/on when _DEBUG_ is defined. */
  20. #endif
  21.  
  22. /*
  23.  * DebugFunc() - same as printf(), but with less formatting capabilities.
  24.  * It will open a console window automatically when the first character
  25.  * is about to be printed.
  26.  */
  27.  
  28. #ifdef _DEBUG_
  29.  
  30. void
  31. DebugFunc(char *fmt, ...)
  32. {
  33.     char *s;
  34.     extern void output(unsigned int);
  35.     va_list ap;
  36.     
  37.     if (debugflag == 0)
  38.         return;
  39.  
  40.     va_start (ap, fmt);
  41.     buf[0] = 0;
  42.     vsprintf(buf, fmt, ap);
  43.     for (s = buf; *s; s++)
  44.         output(*s & 0xff);
  45. }
  46.  
  47.  
  48. void _vst_assert_(s, asrt, file, line)
  49. BOOL s;
  50. char *asrt;
  51. char *file;
  52. int line;
  53. {
  54.     if (s)
  55.         DebugFunc("VST ASSERTION FALIED: file %s line %d ( %s )\n",file,line,asrt);
  56. }
  57.  
  58. void
  59. dowrite()
  60. {
  61.     static int init;
  62.     int r;
  63.     extern void writeaux();
  64.  
  65.     if (!init) {
  66.         init++;
  67.         AllocConsole();
  68.         if ((Console_Handle = GetStdHandle(STD_OUTPUT_HANDLE)) ==
  69.                  INVALID_HANDLE_VALUE) {
  70.             Console_Handle = NULL;
  71.         }
  72.     }
  73.     r = 0;
  74.     if (Console_Handle && WriteFile(Console_Handle, buf, bufp-buf, &r, NULL) ==
  75.         FALSE)
  76.         writeaux();
  77.     else if (r != (bufp-buf))
  78.         writeaux();
  79. }
  80.  
  81. static void
  82. writeaux()
  83. {
  84.     /* Open COM? assuming dumb terminal connected. */
  85. }
  86.  
  87. void
  88. ConsClose()
  89. {
  90.     /* CloseHandle */
  91. }
  92.  
  93. /*
  94.  * Output a character to screen. Uses bios functions.
  95.  * video memory.
  96.  */
  97.  
  98. void
  99. output(c)
  100. unsigned int c;
  101. {
  102.     if (bufp >= endp) {
  103.         dowrite();
  104.         bufp = buf;
  105.     }
  106.     *bufp++ = c;
  107.     if (c == '\n' || c == '\r') {
  108.         dowrite();
  109.         bufp = buf;
  110.     }
  111. }
  112. #endif /* _DEBUG_ */
  113.  
  114. #undef _sanitycheck_debug_
  115. #ifdef _sanitycheck_debug_
  116. main()
  117. {
  118.     void DebugFunc(char *, ...);
  119.     
  120.     DebugFunc("Testing 1 2 3...\n");
  121.     DebugFunc("%d (%%d) %s (%%s) %c (%%c) and then 0x%x and 0x%05x\n",
  122.         123, "character", 'Z', 0x45678978, 0x54321);
  123.     DebugFunc("neg. numbers %d (negative # in %%u) %u %d (%%d)\n", -10, -20, 0xfffa000);
  124.     DebugFunc("pos. large numbers %d (%%d) 0x%x\n", 637848766, 0x7fffffff);
  125.     DebugFunc("neg. large numbers 0x%08x=%d (%%d)\n",0x80000002, 0x80000002);
  126.     DebugFunc("float: (1.2000211) = %f - Double (1.20302020101) = %lf\n", 1.2000211,1.20302020101);
  127. }
  128. #endif 
  129.